<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="equipmentNo">
        <el-input
          v-model="queryParams.equipmentNo"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentType">
        <el-select v-model="queryParams.equipmentType" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.biz_equipmen_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="collegeId">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusOptions" :label="item.statusDesc" :value="item.status"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属实验室" prop="labId">
        <el-select v-model="queryParams.labId" placeholder="请选择所属实验室" clearable>
          <el-option v-for="item in labOptions" :label="item.labName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['biz:equipment:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:equipment:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:equipment:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['biz:equipment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="设备编号" align="center" prop="equipmentNo"/>
      <el-table-column label="设备名称" align="center" prop="equipmentName"/>
      <el-table-column label="所属实验室" align="center" prop="labName"/>
      <el-table-column label="设备类型" align="center" prop="equipmentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_equipmen_type" :value="scope.row.equipmentType"/>
        </template>
      </el-table-column>
      <el-table-column label="使用说明" align="center" prop="usageInstructions"/>
      <el-table-column label="租用价格(元/天)" align="center" prop="price"/>
      <el-table-column label="设备租用状态" align="center" prop="statusDesc"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:equipment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:equipment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实验设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备编号" prop="equipmentNo">
          <el-input placeholder="请输入设备编号" v-model="form.equipmentNo" class="input-with-select">
            <el-button slot="append" @click="generateEquipmentNo()">生成</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="所属实验室" prop="labId">
          <el-select v-model="form.labId" placeholder="请选择所属实验室">
            <el-option v-for="item in labOptions" :label="item.labName" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="equipmentType">
          <el-select v-model="form.equipmentType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.biz_equipmen_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用说明" prop="usageInstructions">
          <el-input v-model="form.usageInstructions" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="租用价格（元/天）" prop="price">
          <el-input v-model="form.price" placeholder="请输入租用价格（单位：元/天）"/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable>
            <el-option v-for="item in statusOptions" :label="item.statusDesc" :value="item.status"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment} from "@/api/system/equipment";
import {listLab} from "@/api/system/lab";

export default {
  name: "Equipment",
  dicts: ['biz_equipmen_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实验设备表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labId: null,
        equipmentName: null,
        equipmentNo: null,
        equipmentType: null,
        usageInstructions: null,
        price: null,
        status: null,
      },
      // 表单参数
      form: {},
      labOptions: [],
      statusOptions: [
        {
          status: 1,
          statusDesc: "正常"
        },
        {
          status: 2,
          statusDesc: "停用"
        },
        {
          status: 3,
          statusDesc: "租用中"
        },
        {
          status: 4,
          statusDesc: "废弃"
        }
      ],
      // 表单校验
      rules: {
        equipmentName: [
          {required: true, message: "设备名称不能为空", trigger: "change"}
        ],
        equipmentType: [
          {required: true, message: "设备类型不能为空", trigger: "change"}
        ],
        status: [
          {required: true, message: "设备租用状态不能为空", trigger: "change"}
        ],
        labId: [
          {required: true, message: "所属实验室不能为空", trigger: "change"}
        ],
        usageInstructions: [
          {required: true, message: "使用说明不能为空", trigger: "change"}
        ],
        price: [
          {required: true, message: "价格不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getLabList();
  },
  methods: {
    /** 查询实验设备列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    generateEquipmentNo() {
      // 生产设备编号
      this.form.equipmentNo = "E" + new Date().getTime();
    },
    getLabList() {
      listLab().then(response => {
        this.labOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        labId: null,
        equipmentName: null,
        equipmentType: null,
        usageInstructions: null,
        price: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        createdBy: null,
        updatedBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实验设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEquipment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改实验设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除实验设备编号为"' + ids + '"的数据项？').then(function () {
        return delEquipment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/equipment/export', {
        ...this.queryParams
      }, `equipment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
